import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Manager',
      component: () => import('@/views/Manager.vue'),
      redirect: '/home',
      children: [
        { path: 'home', name: 'Home', component: () => import('@/views/manager/Home.vue') },
        { path: 'productCategory', name: 'productCategory', component: () => import('@/views/manager/ProductCategory.vue') },
        { path: 'productInfo', name: 'productInfo', component: () => import('@/views/manager/ProductInfo.vue') },
        { path: 'orderMaster', name: 'orderMaster', component: () => import('@/views/manager/OrderMaster.vue') },
        { path: 'passWord', name: 'passWord', component: () => import('@/views/manager/PassWord.vue') },
        { path: 'systemLog', name: 'systemLog', component: () => import('@/views/manager/SystemLog.vue') },
        {path: 'loginLog', name: 'loginLog', component: () => import('@/views/manager/LoginLog.vue')},
      ]
    }, {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: {
        // 不需要登录就能访问的页面
        requiresAuth: false
      }
    }, {
      path: '/:catchAll(.*)',
      name: '404',
      component: () => import('@/views/404.vue')
    }


  ]
})

router.beforeEach(to => {
  // 直接放行的路由 如:登录
  if (to.name === 'Login') {
    return true;
  }
  // 如果没有登录不允许路由到主界面home
  // 统一登录身份认证,  在没有登录的情况不允许进入需要登录身份认证的页面
  // 具体实现方式: 登录成功后，保存token,  在全局前置导航守卫处统一判断
  const token = localStorage.getItem('token')
  if (token) {
    return true // 放行
  } else {
    return { path: '/login' } //重定向到/路由登录界面
  }
})

export default router
